





import excel using "$data_folder/crosswalk_endes_occupations.xlsx", clear firstrow
keep codigo_endes sector1r4
tempfile ocup_xwalk
sa `ocup_xwalk'

//from enaho code 
import excel using "$data_folder//changes_employment_sector.xlsx", clear firstrow
keep sector_number pct_chng_wgt med
ren sector_number sector1r4
tempfile ocup_shock
sa `ocup_shock'





use "$data_folder/endes_stacked.dta", clear

ren V704 occupation_husband
ren V716 occupation_wife

//replace with missing if not working
replace occupation_husband = .  if inlist( occupation_husband, 0,99)
replace occupation_wife = .  if  inlist( occupation_wife, 0,99)

gen earns_less_or_same = inlist(V746, 2, 3)

//set occupation of person who earns the most 
gen occupation_earner = . 
//set mans if wife is missing
replace occupation_earner = occupation_husband 	if occupation_wife == .
// set mans if respondent says man earns more 
replace occupation_earner = occupation_husband 	if earns_less_or_same == 1
//set mans if both respond but there is no info on the comparison 
replace occupation_earner = occupation_husband 	if occupation_husband!= . &  occupation_wife!= .
//set womans if still missing and man's missing
replace occupation_earner = occupation_wife 	if occupation_wife!= . &  occupation_earner== .

ren occupation_earner codigo_endes

tab year

merge m:1 codigo_endes using `ocup_xwalk', gen(ocup_merge)

merge m:1 sector1r4 using `ocup_shock', gen(shock_merge) 

// //merge on one digit 
// tostring codigo_endes, gen(isco_1dig)
// drop if isco_1dig == "0" // these are unemployed in ENDES
// replace isco_1dig = "0"+ isco_1dig if length(isco_1dig) ==1
// replace isco_1dig = substr(isco_1dig,1,1)
// destring isco_1dig, replace
// merge m:1 isco_1dig using "$wfhData", gen(wfh_merge) 
// ren median wfh_median

foreach var in D101A D101B D101C D101D D101E D101F D103A D103B D103C D103D D103E D103F  ///
               D105A D105B D105C D105D D105E D105F D105G  D105H D105I {
	gen l12_`var' = inlist(`var', 1,2) if `var' != . 
}

tab year

* For Daniel, these are the prevalence questions
egen any_controll_last_12 =rowmax(l12_D101A l12_D101B l12_D101C l12_D101D l12_D101E l12_D101F)


//what matches to our questions 
// 103A humiliate in front of others
// 103B 103C   partner insulted, yelled, broken your belongings, threatened to hit youor throw something to you
//we miss yelled and broken belongings
// egen any_psyco_last_12 =rowmax(l12_D103A l12_D103B l12_D103C  l12_D103E l12_D103F)
egen any_psyco_last_12 =rowmax(l12_D103A l12_D103B l12_D103C )

//what matches to our questions 
// 105A matches push, shoved
// 105B  Matches slap twisted arm
// 105C matches hit with fist or object
// this was our old construction 
// egen any_physical_last_12 = rowmax(l12_D105A l12_D105B l12_D105C l12_D105D l12_D105E l12_D105F l12_D105G)

egen any_physical_last_12 = rowmax(l12_D105A l12_D105B l12_D105C )

// 105I 105H for forced sexual relations 
egen any_sexual_last_12 = rowmax(l12_D105H l12_D105I )

egen any_our_physi_last_12 = rowmax(any_sexual_last_12 any_physical_last_12 )

egen any_our_any_last_12 = rowmax(any_our_physi_last_12 any_psyco_last_12 )



egen any_ipv_last_12 = rowmax (any_controll_last_12 any_psyco_last_12 any_physical_last_12 any_sexual_last_12)

split CASEID, parse(" ") 
rename CASEID1 HHID



// in 2019 37474 HHIDs 
**Merging with first modules to get ubigeos
// From  07 endes code/1_ubigeos_endes.do
merge m:1 HHID year using "$data_folder/endes_ubigeos.dta", gen(_m1)
tab _m1 year


keep if _m1 == 3


**Merging with Bartik
preserve
	use "$outcomes_bartik/bartik.dta", clear
	gen ln_bartik_national=ln(bartik_national)
	keep ubigeo year ln_bartik_national
	sort ubigeo year, stable
	bysort ubigeo: gen input=ln_bartik_national[_n-1]
	gen bartik_change=(ln_bartik_national-input)
	*Only shock of April-May 2020, compared to 2019
	keep if year==2020
	keep ubigeo bartik_change 
	label var bartik_change "Difference of ln_bartik2020AM and ln_bartik2019 "
	tempfile bartik
	save `bartik'
restore

tab year


sa "$data_folder/endes_with_enaho_occup", replace 
























